package com.xiaoq.users.emchat.endpoint;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xiaoq.users.emchat.api.IMUserAPI;
import com.xiaoq.users.emchat.comm.ClientContext;
import com.xiaoq.users.emchat.comm.EasemobRestAPIFactory;
import com.xiaoq.users.emchat.comm.body.IMUserBody;
import com.xiaoq.users.emchat.comm.wrapper.BodyWrapper;

/**
 * @author xinpeng created on 2016/12/1-上午11:22.
 */

@RestController
@RequestMapping(value = "/api/v1/user/im")
public class EasemobIMUserEndpoint {

    private static final Logger logger = LoggerFactory.getLogger(EasemobIMUserEndpoint.class);

    @RequestMapping(value = "", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.POST)
    public ResponseEntity<?> registerUserToEasemobIM(
            @RequestParam String userId,
            @RequestParam String password,
            @RequestParam String displayName){

        EasemobRestAPIFactory factory = ClientContext.getInstance().init(ClientContext.INIT_FROM_PROPERTIES).getAPIFactory();

        IMUserAPI user = (IMUserAPI)factory.newInstance(EasemobRestAPIFactory.USER_CLASS);

        // Create a IM user
		BodyWrapper userBody = new IMUserBody(userId, password, displayName);

        Object obj =  user.createNewIMUserSingle(userBody);
        if (null != obj) {
            logger.info("注册IM用户[单个]: " + obj.toString());
        }

        return new ResponseEntity<>(obj, HttpStatus.OK);

    }

}
